var Web = (function($) {
	var tplc = [];
	var debug = false;
	function getdata(data) {
		if (typeof (data) == 'object' && data['returnValue']) {
			return data['returnValue'];
		} else {
			return eval('(' + data + ')');
		}
	}
	function format(tpl, data, cb) {
		var m = tpl.match(/\{(\w+)\}/ig);
		var names = getfields(m);
		var html = '';
		try {
			if (data && data.length) {
				for ( var i = 0; i < data.length; i++) {
					var tmp = tpl;
					for ( var j = 0; j < names.length; j++) {
						var re = new RegExp('{' + names[j] + '}', 'ig');
						if (cb && cb.render) {
							var tmp2 = cb.render(i, tmp, names[j],
									data[i][names[j]]);
							if (tmp2 == tmp) {
								tmp = tmp.replace(re, data[i][names[j]]);
							} else {
								tmp = tmp2;
							}
						} else {
							tmp = tmp.replace(re, data[i][names[j]]);
						}
					}
					html += tmp;
				}
			} else if (data) {
				var tmp = tpl;
				for ( var j = 0; j < names.length; j++) {
					var re = new RegExp('{' + names[j] + '}', 'ig');
					if (cb && cb.render) {
						var tmp2 = cb.render(tmp, names[j], data[names[j]]);
						if (tmp2 == tmp) {
							tmp = tmp.replace(re, data[names[j]]);
						} else {
							tmp = tmp2;
						}
					} else {
						tmp = tmp.replace(re, data[names[j]]);
					}
				}
				html += tmp;
			}
		} catch (e) {
			if (this.debug && console) {
				console.error('check data && return tpl' + e);
			}
		}
		return html;
	}
	function getfields(tags) {
		var names = [];
		for ( var i = 0; i < tags.length; i++) {
			names[i] = tags[i].replace(/\{|\}/ig, '');
		}
		return names;
	}
	function get_tpl(id) {
		for ( var i = 0; i < tplc.length; i++) {
			if (id == tplc[i].id) {
				return tplc[i].tpl;
			}
		}
	}
	function add_tpl(tplid, tpl) {
		if (!tpl) {
			tpl = $(tplid).html();
		}
		for ( var i = 0; i < tplc.length; i++) {
			if (tplc[i].id == tplid) {
				return;
			}
		}
		tplc[i] = {
			'id' : tplid,
			'tpl' : tpl
		};
	}
	function fill(url, cid, tpl, cb) {
		$.get(url, {}, function(ret) {
			var data = getdata(ret);
			if (cb && cb.before) {
				data = cb.before(data);
			}
			if (cid && tpl) {
				var html = format(tpl, data, cb);
				$(cid).html(html);
			}
			if (cb && cb.after) {
				cb.after(data);
			}
		}, 'text');
	}
	
	function pfill(url,data, cid, tpl, cb) {
		$.post(url, data, function(ret) {
			var data = getdata(ret);
			if (cb && cb.before) {
				data = cb.before(data);
			}
			if (cid && tpl) {
				var html = format(tpl, data, cb);
				$(cid).html(html);
			}
			if (cb && cb.after) {
				cb.after(data);
			}
		}, 'text');
	}
	;
	
	function postFill(url,dd, cid, cb){
		var tpl=pre_fill(cid);
		pfill(url,dd, cid,tpl, cb); 
		
	}
	
	function pre_fill(cid){
		var tpl='';
		if (typeof (cid) == 'object') {
			cb = cid;
			cid = '';
		}
		var tt = get_tpl(cid);
		var tpl = '';
		if (tt) {
			tpl = tt;
		} else if (cid) {
			try {
				if ($('.tpl', $(cid)).length > 0) {
					tpl = $('.tpl', $(cid)).parent().html()
							.replace('"tpl', '"');
				} else {
					tpl = $(cid).html().replace('"tpl', '"');
				}
				$(cid).removeClass('tpl').html('');
				tpl = tpl.replace('tpl"', '"').replace('"tpl"', '""').replace(
						' tpl ', '');
				tpl = tpl.replace('tpl\'', '\'').replace('\'tpl\'', '\'\'')
						.replace(' tpl\'', '\'');
				add_tpl(cid, tpl);
			} catch (e) {
				if (this.debug && console) {
					console.error('Error:' + e);
				}
			}

		}
		return tpl;
		
	}
	
	function fill2(url, cid, cb) {
		var tpl=pre_fill(cid);
		fill(url, cid, tpl, cb); 
	}
	function init() {
		$('.tpl').parent().each(function(i) {
			var cid = $(this).attr('id');
			var tpl = $(this).html().replace('tpl', '');
			if (cid)
				add_tpl(cid, tpl);
		});
	}
	function strtotime(str) {
		str = str.indexOf(':') > 0 ? str : str + ' 00:00:00';
		var strArray = str.split(" ");
		var strDate = strArray[0].split("-");
		var strTime = strArray[1].split(":");
		var a = new Date(strDate[0], (strDate[1] - parseInt(1)), strDate[2],
				strTime[0], strTime[1], strTime[2]);
		return a.getTime();
	}
	function date(pat, ts) {
		var y, m, d, H, i, s;
		var dt = [ 'Y', 'm', 'd', 'H', 'i', 's' ];
		var dv = [];
		var dd = ts ? new Date(ts) : new Date();
		y = dd.getYear() + 1900;
		m = dd.getMonth() + 1, m = m < 10 ? '0' + m : m;
		d = dd.getDate(), d = d < 10 ? '0' + d : d;
		H = dd.getHours(), H = H < 10 ? '0' + H : H;
		i = dd.getMinutes(), i = i < 10 ? '0' + i : i;
		s = dd.getSeconds(), s = s < 10 ? '0' + s : s;
		dv[0] = y;
		dv[1] = m;
		dv[2] = d;
		dv[3] = H;
		dv[4] = i;
		dv[5] = s;
		for ( var p in dt) {
			pat = pat.replace(new RegExp(dt[p]), dv[p]);
		}
		return pat;
	}
	function request(paras) {
		var url = location.href;
		var paraString = url.substring(url.indexOf("?") + 1, url.length).split(
				"&");
		var paraObj = {};
		for ( var i = 0; j = paraString[i]; i++) {
			paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j
					.substring(j.indexOf("=") + 1, j.length);
		}
		var ret = paraObj[paras.toLowerCase()];
		if (typeof (ret) == "undefined") {
			return "";
		} else {
			return ret;
		}
	}
	function type(param) {
		var _type = typeof (param);
		return (_type === 'object' ? param === null && 'null'
				|| Object.prototype.toString.call(param).slice(8, -1) : _type)
				.toLowerCase();
	} 
	;
 
	return {
		'init' : init,
		'debug' : debug,
		'fill' : fill2,
		'fill2' : fill,
		'tpl' : tplc,
		'get_tpl' : get_tpl,
		'add_tpl' : add_tpl,
		'strtotime' : strtotime,
		'date' : date,
		'request' : request,
		'format' : format,
		'type' : type,
		'postFill':postFill 
	};

})(jQuery);

(function(object, window, undefined) {
	var topicStack = {};
	var type = function(param) {
		var _type = typeof (param);
		return (_type === 'object' ? param === null && 'null'
				|| Object.prototype.toString.call(param).slice(8, -1) : _type)
				.toLowerCase();
	};
	var namespace = function(set, ns, callback) {
		if (!/\.{2,}|\.$/g.test(ns)
				&& /^[a-zA-Z_\$][a-zA-Z0-9_\.\$]*$/g.test(ns)) {
			var o = topicStack;
			var parentNs = o;
			var arr = ns.split('.');
			var len = arr.length;
			if (set) {
				for ( var i = 0; i < len; i++) {
					var _ns = arr[i];
					if (_ns !== '__Listeners__') {
						parentNs = o;
						o = o[_ns] = o[_ns] || {};
					}
				}
				type(callback) === 'function'
						&& callback.call(o, parentNs, _ns);
			} else {
				for ( var i = 0; i < len; i++) {
					var _ns = arr[i];
					if (_ns !== '__Listeners__') {
						var _targetNs = o[_ns];
						if (_targetNs) {
							parentNs = o;
							o = _targetNs;
							if (i === len - 1) {
								type(callback) === 'function'
										&& callback.call(o, parentNs, _ns);
							}
						} else {
							break;
						}
					}
				}
			}
		} else {
			throw new Error('Topic is not legitimate !');
		}
	};
	var notifyUpdate = function(obj, data) {
		var listeners = obj.__Listeners__ || [];
		for ( var i = 0, listenersLen = listeners.length; i < listenersLen; i++) {
			listeners[i].call(window, data);
		}
		for ( var i in obj) {
			if (i !== '__Listeners__') {
				arguments.callee(obj[i], data);
			}
		}
	};
	var deleteTopic = function(obj) {
		for ( var i in obj) {
			if (i === '__Listeners__') {
				if (obj[i].length) {
					return false;
				} else {
					continue;
				}
			} else {
				return false;
			}
		}
		return true;
	};
	var removeListeners = function(obj, handler, parentNs, ns) {
		var listeners = obj.__Listeners__ || [];
		var cachelisteners = [];
		for ( var i = 0, listenersLen = listeners.length; i < listenersLen; i++) {
			var listener = listeners[i];
			if (listener !== handler) {
				cachelisteners.push(listener);
			}
		}
		obj.__Listeners__ = cachelisteners;
		deleteTopic(obj) && delete parentNs[ns];
	};
	object.subscribe = function(topic, handler) {
		if (topic && handler) {
			var topicType = type(topic);
			var handlerType = type(handler);
			var tempHandler = function() {
				var that = this;
				that.__Listeners__ = that.__Listeners__ || [];
				if (handlerType === 'function') {
					that.__Listeners__.push(handler);
				} else if (handlerType === 'array') {
					for ( var i = 0, handlerLen = handler.length; i < handlerLen; i++) {
						var _handler = handler[i];
						if (type(_handler) === 'function') {
							that.__Listeners__.push(_handler);
						}
					}
				}
			};
			if (topicType === 'string') {
				namespace(true, topic, tempHandler);
			} else if (topicType === 'array') {
				for ( var i = 0, topicLen = topic.length; i < topicLen; i++) {
					var _topic = topic[i];
					if (type(_topic) === 'string') {
						namespace(true, _topic, tempHandler);
					}
				}
			}
		}
	};
	object.publish = function(topic, data) {
		if (topic) {
			var topicType = type(topic);
			var tempHandler = function() {
				var that = this;
				notifyUpdate(that, data);
			};
			if (topicType === 'string') {
				namespace(false, topic, tempHandler);
			} else if (topicType === 'array') {
				for ( var i = 0, topicLen = topic.length; i < topicLen; i++) {
					var _topic = topic[i];
					namespace(false, _topic, tempHandler);
				}
			}
		}
	};
	object.unsubscribe = function(topic, handler) {
		var argsLen = arguments.length;
		if (argsLen) {
			var topicType = type(topic);
			var handlerType = type(handler);
			var tempHandler = function(parentNs, ns) {
				var that = this;
				if (argsLen === 1) {
					delete parentNs[ns];
				} else if (handlerType === 'function') {
					removeListeners(that, handler, parentNs, ns);
				} else if (handlerType === 'array') {
					for ( var i = 0, handlerLen = handler.length; i < handlerLen; i++) {
						var _handler = handler[i];
						if (type(_handler) === 'function') {
							removeListeners(that, _handler, parentNs, ns);
						}
					}
				}
			};
			if (topicType === 'string') {
				namespace(false, topic, tempHandler);
			} else if (topicType === 'array') {
				for ( var i = 0, topicLen = topic.length; i < topicLen; i++) {
					var _topic = topic[i];
					if (type(_topic) === 'string') {
						namespace(false, _topic, tempHandler);
					}
				}
			}
		}
	};
	object.type = type;
})(window.Newton = window.Newton || {}, window);


function obj2str(o){
	  var r = [];
	  if(typeof o =="string") return "\""+o.replace(/([\"\\])/g,"\\$1").replace(/(\n)/g,"\\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\"";
	  if(typeof o == "object"){
	      if(!o.sort){
	          for(var i in o)
	              r.push('"'+i+'":'+obj2str(o[i]));
	          if(!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)){
	              r.push("toString:"+o.toString.toString());
	          }
	          r="{"+r.join()+"}";
	      }else{
	          for(var i =0;i<o.length;i++)
	              r.push(obj2str(o[i]));
	          r="["+r.join()+"]";
	      }
	      return r;
	  }
	  return o.toString();
}





